package com.example.test;


import android.content.Context;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;
/**
 * Custom gallery
 * @author NamLH
 *
 */
public class CustomGallery extends Gallery {
	

	private GestureDetector tapDetector =null;

	public GestureDetector getTapDetector() {
		return tapDetector;
	}
	public void setTapDetector(GestureDetector tapDetector) {
		this.tapDetector = tapDetector;
	}
	public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	public CustomGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public CustomGallery(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		
		/*ThuNA 2012/10/18 - Add S*/
		System.out.print("\n onFling()ThuNA +++++++++++++++++++++++ mOnWay" + mMoveOneWay);
		// Check null pointer
		if(e1 == null || e2 == null || mMoveOneWay == false){
			mMoveOneWay = true;
			return false;
		}
		// Get the distance that user move on screen
		float distance =e1.getX() - e2.getX();
		
		// Calculate the threshold to fling
		double curThreshold = ((double)distance) * ((double)velocityX);
		
		// In case it doesn't exceed the threshold then keep the current image
		if(Math.abs(curThreshold) < FLING_THRESHOLD_MAXIMUM){
			mMoveOneWay = true;
			return false;
		}
		
		mMoveOneWay = true;
		int kEvent;
		
		// User swipes from left to right
	    if(isScrollingLeft(e1, e2)){ //Check if scrolling left
	    	kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
	    }else{ //Otherwise scrolling right
		  	kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
	    }
	    
	    // Call key listener function as flinging as screen
	    onKeyDown(kEvent, null);
	    return true;
	    
	    /*ThuNA 2012/10/18 - Add E*/
	}
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
        return e2.getX() > e1.getX();
    }
	
	private float mStartX=0;
	private float mStartY= 0;
	
	/*ThuNA 2012/10/17 - Add S*/
	// Flag is to know user finished moving or fling
	private boolean mMoveOneWay = true;
	
	// Variable to ensure the direction of moving
	private float mLastMovingX = 0.0f;
	private float mNextMovingX = 0.0f;
	private float mLastDeltaX = 0.0f;
    /** Maximum velocity for fling */
    private final static double FLING_THRESHOLD_MAXIMUM = 100000.0;//200000.0;
    
	/*ThuNA 2012/10/17 - Add E*/
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		return super.onTouchEvent(event);		
	}
	
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return super.onScroll(e1, e2, distanceX, distanceY);
	}
	@Override
	protected void onAnimationEnd() {
		// TODO Auto-generated method stub
		System.out.println("----------- ANIMATION END ----------------");
		super.onAnimationEnd();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		System.out.println("NAMLH screen size "+widthMeasureSpec+";"+widthMeasureSpec);
	}
	/*ThuNA 2012/10/23 ADD-S*/
	@Override
	public boolean isSoundEffectsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/*ThuNA 2012/10/23 ADD-S*/
	
}
